package com.yz.smartpos.yztester.util;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;

import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import com.yz.smartpos.yztester.ui.MainActivity;
import com.yz.smartpos.yztester.ui.OperateFragment;
import com.yz.smartpos.yztester.ui.ResultFragment;

/**
 * Created by zhuxy on 2017/9/7.
 */
public class YzTestRunner extends RunListener implements Runnable {
    private final static String TAG = "YzTestRunner";
    private ArrayList<TestCase> mTestCaseList = new ArrayList<TestCase>();
    protected static MainActivity mActivity = null;
    protected static OperateFragment mFragment = new OperateFragment();
    private static ResultFragment Logger = ResultFragment.newInstance();
    private final SimpleDateFormat sdf = new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss]", Locale.getDefault());

    private int startIndex = 0;
    private boolean stopTest = false;
    private int errrcnt = 0;
    private boolean isStopTest = false;
    private int runcnt = 0;

    private YzTestRunner mYzTestRunner;

    public YzTestRunner(ArrayList<TestCase> testCasesList, int startIndex) {
        mTestCaseList = testCasesList;
        this.startIndex = startIndex;
        runcnt = 0;
        this.mYzTestRunner = this;
    }

    public YzTestRunner(TestCase ApiTestCase, int cnt) {
        mTestCaseList.clear();
        runcnt = cnt;
        for (int i = 0; i < cnt; i++)
            mTestCaseList.add(ApiTestCase);
        startIndex = 0;
        this.mYzTestRunner = this;
    }

    public YzTestRunner(TestCase ApiTestCase) {
        mTestCaseList.clear();
        mTestCaseList.add(ApiTestCase);
        TesterState.mCurrentCase = ApiTestCase;
        startIndex = 0;
        runcnt = 0;
        this.mYzTestRunner = this;
    }


    public static void setActivity(Activity activity){
        mActivity = (MainActivity) activity;
    }

    public void run() {
        for(TestCase testcase : mTestCaseList){
            mActivity.mMsgLogFragment.showTitle(testcase.getCaseName());
            TesterState.mCurrentCaseName = testcase.getCaseName();
            TesterState.mCurrentCase = testcase;
            Log.d(TAG, String.format("inter index1=%d index2=%d ModuleName=%s casename=%s", testcase.index1, testcase.index2, testcase.getModuleName(), testcase.getCaseName()));
            JUnitCaseRunner jUnitCaseRunner = new JUnitCaseRunner("com.yz.smartpos.yztester.apifuntest.tests",
                    "com.yz.smartpos.yztester.apifuntest.tests." + testcase.getModuleName(), testcase.getCaseName(), mYzTestRunner);
            Bundle bundle = jUnitCaseRunner.execute();
        }
    }

    @Override
    public void testFinished(Description description) throws Exception {
        super.testFinished(description);
        String test_result = "";
        int errCount = 0;
        if (description.getMethodName().equals("warning")){
            test_result = "NOTFound";
            TesterState.mCurrentCase.setmResultState(TestCase.STATUS_NOTFOUND);
        }else if(TesterState.mCurrentCaseName.equals(TesterState.mCurrentCase.getCaseName())){
            errCount = TesterState.mCurrentCase.getErrCount();
            if(errCount == 0){
                TesterState.mCurrentCase.setmResultState(TestCase.STATUS_PASSED);
                TesterState.caseList_suc.add(TesterState.mCurrentCase);
                test_result = "PASS";
            }else{
                TesterState.mCurrentCase.setmResultState(TestCase.STATUS_FAILED);
                TesterState.caseList_fail.add(TesterState.mCurrentCase);
                test_result = "FAIL fail_cnt=" + errCount;
            }
            TesterState.caseList_runned.add(TesterState.mCurrentCase);
            TesterConfig.writeCaseRunned(TesterState.mCurrentCase);
        }else{
            Log.e(TAG, "TesterState.mCurrentCaseName  " + TesterState.mCurrentCaseName + "\nTesterState.mCurrentCase.getCaseName():" + TesterState.mCurrentCase.getCaseName());
        }

        Message msg2 = new Message();
        msg2.what = 0;
        msg2.arg1 = TesterState.mCurrentCase.index1;
        mFragment.mhandler.sendMessage(msg2);
        Logger.appendLog("EndTime:" + sdf.format(new Date()));
        Logger.appendLog("//END:" + test_result + "\n");
        Logger.scrollToBottom();
        TesterState.setRunState(TesterState.TEST_IDIE);
        Log.d(TAG, "===================testFinished errCount:" + errCount);
        Log.d(TAG, "description:" + description.toString());
        Log.d(TAG, "description:" + description.getClassName());
        Log.d(TAG, "description:" + description.getMethodName());
        Log.d(TAG, "description:" + description.getTestClass());
        Log.d(TAG,"\n\n");
    }

    @Override
    public void testFailure(Failure failure) throws Exception {
        super.testFailure(failure);
    }

    @Override
    public void testRunFinished(Result result) throws Exception {
        super.testRunFinished(result);
        Utils.saveTestResult();
        Log.i(TAG, "testRunFinished:" + result.wasSuccessful());
        Log.i(TAG, "testRunFinished:" + result.getClass());
        Log.i(TAG, "testRunFinished:" + result.getFailureCount());
        Log.i(TAG, "testRunFinished:" + result.getFailures());
//        TesterConfig.tearDir();
    }

    @Override
    public void testIgnored(Description description) throws Exception {
        super.testIgnored(description);
    }

    @Override
    public void testRunStarted(Description description) throws Exception {
        super.testRunStarted(description);
    }

    @Override
    public void testStarted(Description description) throws Exception {
        super.testStarted(description);
        Log.d(TAG, "\n\n=======================testStarted: " + description.getMethodName());
        Log.d(TAG, "description:"+description.toString());

        TesterState.setRunState(TesterState.TEST_RUNNING);
        Logger.appendLog("\n\n//CaseName:" + TesterState.mCurrentCase.getCaseName());
        String startTime = sdf.format(new Date());
        Logger.appendLog("StartTime:" + startTime);
//        Logger.appendLog("SdkBer:" + TesterConfig.sdkVerCode + " " + TesterConfig.sdkVerName);
//        Logger.appendLog("ApVer_inner:" + TesterConfig.InnerVersion);

        TesterState.errorCount = 0;
        TesterState.mCurrentCase.setErrCount(0);
        TesterState.mCurrentCase.lastestRuntime = startTime;
    }
}